package io.framework.modules.soil.utils;

import io.framework.common.utils.DateUtils;
import io.framework.modules.soil.dto.SoilDTO;
import io.framework.modules.water.utils.WaterFileCommonUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 〈土壤工具类  读取数据〉
 *
 * @Author GJQ
 * @Create 2020/5/25
 * @Since 1.0.0
 */

public class SoilFileCommonUtil {

    public static void main(String[] args) {
        String filePath = "E:\\workspace\\projects_rongmo\\资料\\三亚环保项目\\Aaa第二版-新数据\\环保局数据\\2018农村土壤-生态室-2020-05-29\\";
        filePath += "2019年三亚市农村土壤环境质量监测结果表.docx";

        System.out.println(getSoilDTOInfo(filePath, "2020-03").toString());
    }

    public static List<SoilDTO> getSoilDTOInfo(String filePath, String month) {
        //获取第一个表格数据
        int set = 1;
        List<Map<String, Object>> mapList = WaterFileCommonUtil.readWordData(filePath, set, 1, 1, 0);
        List<SoilDTO> list = new ArrayList<>();
        SoilDTO dto = null;
        String name = "";
        for (int i = 0; i < mapList.size(); i++) {
            dto = new SoilDTO();
            Map<String, Object> map = mapList.get(i);
            int k = 0;
            if(StringUtils.isNotBlank(WaterFileCommonUtil.getStringValue(map,k))){
                name = WaterFileCommonUtil.getStringValue(map,k);
            }
            dto.setVillageName(getVillageNameValue(name));
            k++;
            dto.setX(getBigDecimalValueX(name));
            dto.setY(getBigDecimalValueY(name));
            dto.setPointName(WaterFileCommonUtil.getStringValue(map,k++));
            dto.setType(WaterFileCommonUtil.getStringValue(map,k++));
            dto.setPass(WaterFileCommonUtil.getStringValue(map,k++));
            dto.setLevel(WaterFileCommonUtil.getStringValue(map,k++));
            dto.setRemark(WaterFileCommonUtil.getStringValue(map,k));
            dto.setMonth(DateUtils.parse(month,DateUtils.DATE_YEAR_PATTERN));
            dto.setDrRemarks(map.get("remarks").toString().trim());
            list.add(dto);
        }
        return list;
    }

    public static BigDecimal getBigDecimalValueX(String v){
        v.replaceAll("\\)","）").replaceAll("\\(","）").replaceAll(",","，");
        if(v.contains("E")){
            v= v.substring(v.indexOf("E")+1,v.indexOf("，"));
        }else{
            return null;
        }
        return new BigDecimal(v);
    }

    public static BigDecimal getBigDecimalValueY(String v){
        v.replaceAll("\\)","）").replaceAll("\\(","）").replaceAll(",","，");
        if(v.contains("N")){
            v= v.substring(v.indexOf("N")+1,v.indexOf("）"));
        }else{
            return null;
        }
        return new BigDecimal(v);
    }

    public static String getVillageNameValue(String v){
        if(v.contains("（")){
            v= v.substring(0,v.indexOf("（"));
        }
        return v;
    }
}
